import Immutable, {Map, fromJS} from 'immutable';
import {expect} from 'chai';
import reducer from '../src/reducer';
import * as Actions from '../src/actions';

const E1 = 'Trainspotting';
const E2 = '28 Days Later';
const E3 = 'Sunshine';
const E4 = 'Millions';
const E5 = '127 Hours';

describe('reducer', ()=> {
    it('handles SET_ENTRIES', ()=> {
        const initialState = Map();
        const action = Actions.setEntries([E1]);
        const nextState = reducer(initialState, action);

        expect(nextState).to.equal(fromJS({
            entries: [E1]
        }))
    });
    it('handles NEXT', ()=> {
        const initialState = fromJS({
            entries: [E1, E2]
        });
        const action = Actions.next();
        const nextState = reducer(initialState, action);

        expect(nextState).to.equal(fromJS({
            vote: {
                pair: [E1, E2]
            },
            entries: []
        }))
    });
    it('handles VOTE', ()=> {
        const initialState = fromJS({
            vote: {
                pair: [E1, E2]
            },
            entries: []
        });
        const action = Actions.vote(E1);
        const nextState = reducer(initialState, action);
        expect(nextState).to.equal(fromJS({
            vote: {
                pair: [E1, E2],
                tally: {[E1]: 1}
            },
            entries: []
        }));
    });
    it('has an initial state', ()=> {
        const action = Actions.setEntries([E1]);
        const nextState = reducer(undefined, action);
        expect(nextState).to.equal(fromJS({
            entries: [E1]
        }));
    });
    it('can be used with reduce', ()=> {
        const actions = [
            Actions.setEntries([E1, E2]),
            Actions.next(),
            Actions.vote(E1),
            Actions.vote(E2),
            Actions.vote(E1),
            Actions.next()
        ];
        const finalState = actions.reduce(reducer, Map());
        expect(finalState).to.equal(fromJS({
            winner: E1
        }));
    })
});